/**
 * @author 雷明浩
 * @date 2025/5/29 10:22
 */
public class AddTwoNumbers {
    // leetcode 两数相加
    // https://leetcode.cn/problems/add-two-numbers/?envType=study-plan-v2&envId=top-100-liked
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode cur = head;
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        int tmp = 0;
        while (cur1 != null ||cur2 != null || tmp != 0) {
            int n1 = cur1 == null ? 0 : cur1.val;
            int n2 = cur2 == null ? 0 : cur2.val;
            int sum = n1 + n2 + tmp;
            tmp = sum / 10;
            ListNode newNode = new ListNode(sum % 10);
            cur.next = newNode;
            cur = cur.next;
            cur1 = cur1 == null ? null : cur1.next;
            cur2 = cur2 == null ? null : cur2.next;
        }
        return head.next;
    }
}